package com.dapeng.www.recycletest;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    private SwipeRefreshLayout id_swipRefreshLayout;
    private RecyclerView recycle;

    private LinearLayoutManager linearLayoutManager;

    private RefreshFootAdapter refreshFootAdapter;

    /**
     * 最后一个看到的item
     */
    private int lastVisibleItem;

    private TextView tv_tablelayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.tv_tablelayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,RadioButtonTableActivity.class);
                startActivity(intent);
            }
        });



        recycle = (RecyclerView) findViewById(R.id.recycle);
        id_swipRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipRefreshLayout);

        id_swipRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        id_swipRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);

        id_swipRefreshLayout.setProgressViewOffset(false,0,100);

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);


        refreshFootAdapter = new RefreshFootAdapter(MainActivity.this);

        recycle.setLayoutManager(linearLayoutManager);
        recycle.addItemDecoration(new AdvanceDecoration(this,OrientationHelper.VERTICAL));
        recycle.setAdapter(refreshFootAdapter);

        if (refreshFootAdapter.getItemCount()<6){
            refreshFootAdapter.changeMoreStatus(refreshFootAdapter.PULLUP_NOMORE_DATA);
        }

        id_swipRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<String> newDatas = new ArrayList<String>();
                        for (int i = 0; i < 5; i++) {
                            int index = i + 1;
                            newDatas.add("new item" + index);
                        }
                        refreshFootAdapter.addItem(newDatas);
                        id_swipRefreshLayout.setRefreshing(false);
                    }
                },5000);
            }
        });

        recycle.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (refreshFootAdapter.getLoading_more_status() == refreshFootAdapter.PULLUP_NOMORE_DATA){
                    return;
                }

                if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem+1) == refreshFootAdapter.getItemCount()){
                    refreshFootAdapter.changeMoreStatus(refreshFootAdapter.LOADING_MORE);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            List<String> newDatas = new ArrayList<String>();
                            for (int i = 0; i < 5; i++) {
                                int index = i + 1;
                                newDatas.add("new item" + index);
                            }
                            refreshFootAdapter.addMoreItems(newDatas);
                            refreshFootAdapter.changeMoreStatus(refreshFootAdapter.PULLUP_LOAD_MORE);
                        }
                    },5000);
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });




    }




}
